<%--
	JSP page to gather address details of the user.

	Change Log
	----------
 	Nov 23,2006 created
--%>

<%@ page import="merlin.beans.*" errorPage="mr_errhandler.jsp" %>

<jsp:useBean id="iwRegBean" class="merlin.beans.IWRegistrationBean" scope="session"/>
<jsp:setProperty name="iwRegBean" property="*"/>

<html>
<head>
	<title>Address Details</title>
	<script>

		function goToReportSelectionPage(form){
			<%
				if(!iwRegBean.isPdfDownloadPurchase()){
			%>
				form.action='mr_reportSelection.jsp?<%=request.getQueryString()%>';
				form.submit();
			<%
				}else{
			%>
				document.location.href="iwDownloadFullReports.jsp?sectorids=<%=iwRegBean.getHidReportIds()%>";
			<%
				}
			%>
		}

		function goToCreditCardsDetailsPage(form){
			if(validateForm(form)){
				form.action='mr_creditCardDetails.jsp?<%=request.getQueryString()%>';
				form.submit();
			}
		}

		function isBlank(str){
			var len = str.length;
			if(len==0)return true;	// zero-length string
			for(i=0;i<len;i++){
				if(str.charAt(i) !=' ' && str.charAt(i) !='\n')return false; //some character is there
			}
			return true;
		}

		function isValidEMailFormat(str){
			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			if (str.indexOf(at)==-1){
				return false
			}

			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				return false
			}

			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				return false
			}

			if (str.indexOf(at,(lat+1))!=-1){
				return false
			}

			if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
				return false
			}

			if (str.indexOf(dot,(lat+2))==-1){
				return false
			}

			if (str.indexOf(" ")!=-1){
				return false
			}

			// no tabs
			if(str.indexOf("\t") != -1){
				return false;
			}

			// doesn't end with dot.
			if(str.substring(lstr-1) == "."){
				return false;
			}

			return true;
		}


		function radSameDetailsClick(frm){
			document.getElementById("tblCardHolderDetails").style.visibility = (frm.radSameDetails[0].checked ? "hidden" : "visible");
		}

		function validateForm(frm){
			// sanity checks
			if(isBlank(frm.txtTitle.value)){
				alert("Title cannot be left blank");
				frm.txtTitle.focus();
				return false;
			}
			if(isBlank(frm.txtFirstName.value)){
				alert("First Name cannot be left blank");
				frm.txtFirstName.focus();
				return false;
			}
			if(isBlank(frm.txtSurname.value)){
				alert("Surname cannot be left blank");
				frm.txtSurname.focus();
				return false;
			}
			if(isBlank(frm.txtPosition.value)){
				alert("Position cannot be left blank");
				frm.txtPosition.focus();
				return false;
			}
			if(isBlank(frm.txtCompanyName.value)){
				alert("Company Name cannot be left blank");
				frm.txtCompanyName.focus();
				return false;
			}

			if(isBlank(frm.txtAddress1.value)){
				alert("Address Line 1 cannot be left blank");
				frm.txtAddress1.focus();
				return false;
			}

			if(isBlank(frm.txtTown.value)){
				alert("Town cannot be left blank");
				frm.txtTown.focus();
				return false;
			}

			if(isBlank(frm.txtPostcode.value)){
				alert("Postcode cannot be left blank");
				frm.txtPostcode.focus();
				return false;
			}

			if(isBlank(frm.txtEMailAddress.value)){
				alert("EMail Address cannot be left blank");
				frm.txtEMailAddress.focus();
				return false;
			}
			if(! isValidEMailFormat(frm.txtEMailAddress.value)){
				alert("EMail Address is not valid");
				frm.txtEMailAddress.focus();
				return false;
			}
			// If the credit card details are different, check those values too
			if(frm.radSameDetails[1].checked){
				if(isBlank(frm.txtCardTitle.value)){
					alert("Title cannot be left blank");
					frm.txtCardTitle.focus();
					return false;
				}
				if(isBlank(frm.txtCardForename.value)){
					alert("First Name cannot be left blank");
					frm.txtCardForename.focus();
					return false;
				}
				if(isBlank(frm.txtCardSurname.value)){
					alert("Surname cannot be left blank");
					frm.txtCardSurname.focus();
					return false;
				}
				if(isBlank(frm.txtCardPosition.value)){
					alert("Position cannot be left blank");
					frm.txtCardPosition.focus();
					return false;
				}
				if(isBlank(frm.txtCardCompanyName.value)){
					alert("Company Name cannot be left blank");
					frm.txtCardCompanyName.focus();
					return false;
				}
				if(isBlank(frm.txtCardAddress1.value)){
					alert("Address Line 1 cannot be left blank");
					frm.txtCardAddress1.focus();
					return false;
				}

				if(isBlank(frm.txtCardTown.value)){
					alert("Town cannot be left blank");
					frm.txtCardTown.focus();
					return false;
				}

				if(isBlank(frm.txtCardPostcode.value)){
					alert("Postcode cannot be left blank");
					frm.txtCardPostcode.focus();
					return false;
				}
			}
			return true;
		}

	</script>

</head>

<body>
    <jsp:include page="mr_header.jsp"/>
    <p/>
	<table align="center" width="750px" border="0">
		<tr>
			<td colspan="2" align="center" style='color:white;height:23px;font: 11px verdana,arial, sans-serif ;background:url("mr_images/LongBars-orange750.jpg") no-repeat;' >
				<h4 style="vertical-align:middle">Address Details</h4>
			</td>
		</tr>
	</table>
	<div align="center">

	<table cellpadding="0" cellspacing="0" style="width: 750px;	font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 11px;">
		<tr>
			<td>
				<form name="frmCreditCardDetails" method="post" action="merlin" >

					<input type="hidden" name="mode" value="saveNewOrder">
					<input type="hidden" name="hidSelectedReports" value="">
					<input type="hidden" name="emailSubject" value='New Order From <%=request.getParameter("emailFrom")%>'>

					<table style="width: 750px;	font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 11px;">
						<tr>
						<td>
      					<table >
							<td width="80%">
								<table width="100%" cellpadding="0" cellspacing="0" style="font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 11px;">
									<tr>
										<td style="width: 280px;">&nbsp;</td>
										<td >&nbsp;</td>
									</tr>
									<tr>
										<td style="width: 280px;">&nbsp;</td>
										<td ><span class="boldGreen">Delivery Details</span></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Title (Mr/Mrs/Dr etc)</td>
										<td><input type="text" name="txtTitle" style="width: 50px;" value='<jsp:getProperty name="iwRegBean" property="txtTitle"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> First Name</td>
										<td><input type="text" name="txtFirstName" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtFirstName"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Surname</td>
										<td><input type="text" name="txtSurname" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtSurname"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Position (eg Director)</td>
										<td><input type="text" name="txtPosition" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtPosition"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Company Name</td>
										<td><input type="text" name="txtCompanyName" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCompanyName"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Address Line 1</td>
										<td><input type="text" name="txtAddress1" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtAddress1"/>'></td>
									</tr>
									<tr>
										<td><span style="color: white;">*</span> Address Line 2</td>
										<td><input type="text" name="txtAddress2" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtAddress2"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Town</td>
										<td><input type="text" name="txtTown" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtTown"/>'></td>
									</tr>
									<tr>
										<td><span style="color: white;">*</span> County</td>
										<td><input type="text" name="txtCounty" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCounty"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Postcode (Country if outside of UK - first 8 letters) </td>
										<td><input type="text" name="txtPostcode" maxlength="8" style="width: 100px;text-transform:uppercase" value='<jsp:getProperty name="iwRegBean" property="txtPostcode"/>'></td>
									</tr>
									<tr>
										<td><span style="color: white;">*</span> Telephone</td>
										<td><input type="text" name="txtTelephone" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtTelephone"/>'></td>
									</tr>
									<tr>
										<td><span style="color: #B1730C;">*</span> Email Address</td>
										<%
											// A complicated situation here :
											// The email address can come from either the user typing it or being retrieved
											// from the database for an existing user . So both validations will be checked,
											// with the user-filled email taking precedence.
											String emailAddress = (String)session.getAttribute("emailAddress");
											if(iwRegBean.getTxtEMailAddress() != null && iwRegBean.getTxtEMailAddress().length() > 0){
												emailAddress = iwRegBean.getTxtEMailAddress();
											}
											if(emailAddress == null) emailAddress = "";
										%>
										<td><input type="text" name="txtEMailAddress" value='<%= emailAddress %>' style="width: 150px;"></td>
									</tr>
								</table>
							</td>
				  			<td width="20%">
								<!-- table for cardholder details if different -->
								<%
									String cardDetailsVisiblity = "F".equals(iwRegBean.getRadSameDetails()) ? "visible" : "hidden" ;
								%>
								<table width="100%" cellpadding="0" cellspacing="0" name="tblCardHolderDetails" style="visibility: <%= cardDetailsVisiblity %>;font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 11px;" id="tblCardHolderDetails">
									<tr>
										<td >&nbsp;</span></td>
									</tr>
									<tr>
										<td >&nbsp;</span><span class="boldGreen">Card Address </span></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardTitle" style="width: 50px;" value='<jsp:getProperty name="iwRegBean" property="txtCardTitle"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardForename" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardForename"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardSurname" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardSurname"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardPosition" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardPosition"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardCompanyName" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardCompanyName"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardAddress1" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardAddress1"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardAddress2" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardAddress2"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardTown" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardTown"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardCounty" style="width: 150px;" value='<jsp:getProperty name="iwRegBean" property="txtCardCounty"/>'></td>
									</tr>
									<tr>
										<td ><input type="text" name="txtCardPostcode" maxlength="8" style="width: 100px;text-transform:uppercase" value='<jsp:getProperty name="iwRegBean" property="txtCardPostcode"/>'></td>
									</tr>
								</table>
							</td>
						</table>
						</td>
						<td>
							<!-- SECURE SEAL WILL GO HERE -->
<script type="text/javascript" src="https://seal.thawte.com/getthawteseal?host_name=www.merlinscottassociates.co.uk&amp;size=M&amp;lang=en"></script>						
</td>
						</tr>
						<tr>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>
						<tr>
							<td>
								<table cellpadding="0" cellspacing="0" style="font-family: Verdana, Helvetica, Arial, sans-serif;font-size: 11px;">
									<tr>
										<td height="22" style="width: 150px;">&nbsp;</td>
										<td height="22" style="width: 200px;">&nbsp;</td>
									</tr>
									<tr>
										<td>
											<span style="color: #B1730C;">*</span> Cardholder details<br />
											<span style="color: white;">*</span> are as above?
										</td>
										<td>
											<input type="radio" name="radSameDetails" value="T" <%= cardDetailsVisiblity.equals("hidden")? "checked" : "" %> onclick="radSameDetailsClick(this.form)">Yes
                      						<input type="radio" name="radSameDetails" value="F" <%= cardDetailsVisiblity.equals("visible")? "checked" : "" %> onclick="radSameDetailsClick(this.form)">No
										</td>
									</tr>
								</table>
							<!-- Credit Card Details (Card Options NOT Address) -->
							</td>
              				<td>
							</td>
						</tr>
						<tr>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>
						<tr>
							<td colspan="2">
								<input name="btnBack" class="input" value="Back" style="font-size: 11px; width: 130px;" type="button" onclick="goToReportSelectionPage(this.form)">
								<input name="btnNext" type="button" class="input" value="Next" style="font-size: 11px; width: 130px;" onclick="goToCreditCardsDetailsPage(this.form)">
							</td>
						</tr>
					</table>
				</form>
			</div>
		</td>
	</tr>
	</table>

	<jsp:include page="mr_footer.jsp"/>
</body>

</html>
